<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="PragmaticScala">
<meta name="generator" content="Paradox, paradox-material-theme=0.5.1, mkdocs-material=3.0.3">

<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">


<meta name="description" content="PragmaticScala">
<link rel="shortcut icon" href="../assets/images/favicon.png">
<title>第2章——体验Scala · </title>
<link rel="stylesheet" href="../assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="../lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="../lib/prettify/prettify.css">
<script src="../assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="../assets/fonts/font-awesome.css">
<link rel="stylesheet" href="../assets/fonts/material-icons.css">
<link rel="stylesheet" href="../assets/stylesheets/paradox-material-theme.css">
</head>
<body
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../index.html" title="" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
</span>
<span class="md-header-nav__topic">
第2章——体验Scala
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">&#xE5CD;</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>

</div>
</div>
</nav>
</header>

<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="../index.html" title="" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="../index.html" title="">
</a>
</label>
<ul>
  <li><a href="../contents.html" class="page">目录</a>
  <ul>
    <li><a href="../abstract.html" class="page">内容提要</a></li>
    <li><a href="../part1.html" class="page">第一部分 小试牛刀</a></li>
    <li><a href="../part2.html" class="page">第二部分 深入 Scala</a></li>
    <li><a href="../part3.html" class="page">第三部分 Scala 中的并发编程</a></li>
    <li><a href="../part4.html" class="page">第四部分 Scala 实战</a></li>
    <li><a href="../refs/references.html" class="page">参考书目</a></li>
    <li><a href="../extras.html" class="page">额外指引</a></li>
    <li><a href="../errata.html" class="page">勘误</a></li>
  </ul></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../chapter-02/index.html#第2章-体验scala" class="header">第2章——体验Scala</a>
  <ul>
    <li><a href="../chapter-02/index.html#2-1-使用-repl" class="header">2.1 使用 REPL</a></li>
    <li><a href="../chapter-02/index.html#2-2-命令行上的-scala" class="header">2.2 命令行上的 Scala</a></li>
    <li><a href="../chapter-02/index.html#2-3-以独立脚本方式运行-scala-代码" class="header">2.3 以独立脚本方式运行 Scala 代码</a></li>
    <li><a href="../chapter-02/index.html#2-4-编译-scala" class="header">2.4 编译 Scala</a></li>
  </ul></li>
</ul>
</nav>

</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 1.0.0
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../chapter-02/index.html#第2章-体验scala" class="header">第2章——体验Scala</a>
  <ul>
    <li><a href="../chapter-02/index.html#2-1-使用-repl" class="header">2.1 使用 REPL</a></li>
    <li><a href="../chapter-02/index.html#2-2-命令行上的-scala" class="header">2.2 命令行上的 Scala</a></li>
    <li><a href="../chapter-02/index.html#2-3-以独立脚本方式运行-scala-代码" class="header">2.3 以独立脚本方式运行 Scala 代码</a></li>
    <li><a href="../chapter-02/index.html#2-4-编译-scala" class="header">2.4 编译 Scala</a></li>
  </ul></li>
</ul>
</nav>

</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#第2章-体验scala" name="第2章-体验scala" class="anchor"><span class="anchor-link"></span></a>第2章——体验Scala</h1>
<p>令人惊喜的是，无论是创建一个简短的脚本还是一个完整的企业级应用，都可以轻松地 用 Scala 代码实现并运行。你可以使用任何 IDE，也可以只使用轻量级的编辑器。</p>
<h2><a href="#2-1-使用-repl" name="2-1-使用-repl" class="anchor"><span class="anchor-link"></span></a>2.1 使用 REPL</h2>
<pre class="prettyprint"><code class="language-output">Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172).
Type in expressions for evaluation. Or try :help.

scala&gt;</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunREPL.output#L2-L5">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-output">scala&gt; val number = 6
number: Int = 6</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunREPL1.output#L2-L3">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-output">scala&gt; number = 7
&lt;console&gt;:11: error: reassignment to val
       number = 7
              ^</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunREPL2.output#L2-L5">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-output">scala&gt; val number = 7
number: Int = 7</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunREPL3.output#L2-L3">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-output">scala&gt; val list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunREPL4.output#L2-L3">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-output">scala&gt; def isPalindrome(str: String) =
     |   str == str.reverse
isPalindrome: (str: String)Boolean

scala&gt; isPalindrome(&quot;mom&quot;)
res0: Boolean = true

scala&gt; isPalindrome(&quot;dude&quot;)
res1: Boolean = false

scala&gt; :quit</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunREPL5.output#L2-L12">Full source at GitHub</a>
<h2><a href="#2-2-命令行上的-scala" name="2-2-命令行上的-scala" class="anchor"><span class="anchor-link"></span></a>2.2 命令行上的 Scala</h2>
<pre class="prettyprint"><code class="language-scala">println(&quot;Hello World, Welcome to Scala&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/HelloWorld.scala#L21">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">&gt;scala HelloWorld.scala
Hello World, Welcome to Scala</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunHelloWorld.output#L2-L3">Full source at GitHub</a>
<h2><a href="#2-3-以独立脚本方式运行-scala-代码" name="2-3-以独立脚本方式运行-scala-代码" class="anchor"><span class="anchor-link"></span></a>2.3 以独立脚本方式运行 Scala 代码</h2>
<pre class="prettyprint"><code class="language-sh">#!/usr/bin/env scala

println(&quot;Hello &quot; + args(0))</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/hello.sh">Full source at GitHub</a>
<p><em>运行命令</em></p>
<pre class="prettyprint"><code class="language-sh">hello.sh Buddy</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/RunHello.sh#L4">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Hello Buddy</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunHello.output#L2">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-bat">echo off

cls
call scala %1
pause</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/RunScala.bat">Full source at GitHub</a>
<h2><a href="#2-4-编译-scala" name="2-4-编译-scala" class="anchor"><span class="anchor-link"></span></a>2.4 编译 Scala</h2>
<blockquote>
  <p>FirstStep/Sample.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">object Sample extends App {
  println(&quot;Hello Scala&quot;)
}</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/Sample.scala#L20-L22">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">&gt; scalac Sample.scala
&gt; scala Sample
Hello Scala
&gt; java -classpath /opt/scala/current/lib/scala-library.jar:. Sample
Hello Scala</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter2/shoutput/RunSample.output#L2-L6">Full source at GitHub</a>
</div>
<div>
<a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/master/src/paradox/chapter-02/index.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
1.0.0
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../chapter-01/index.html" title="第1章——探索Scala" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
第1章——探索Scala
</span>
</div>
</a>
<a href="../chapter-03/index.html" title="第3章——从Java到Scala" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
第3章——从Java到Scala
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>

</div>
</div>
</div>
</footer>

</div>
<script src="../assets/javascripts/application.583bbe55.js"></script>
<script src="../assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"../."}})</script>
<script type="text/javascript" src="../lib/prettify/prettify.js"></script>
<script type="text/javascript" src="../lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>
